<?php
/**
 * Created by PhpStorm.
 * User:雨寒
 * Date: 2018/12/20
 * Time: 9:51
 */

namespace app\admin\controller;

use think\Exception;
use think\Db;
use app\common\model\Message;
class Custom extends Common
{
    public function index(){
        //标题
        $this->templateTitle("自定义单页");
        $title=input('title');
        $this->assign('title',$title);
        $where['id'] = ['gt',0];

        //获取所有用户
        $list = Db::name("custom_page")
            ->where($where)
            ->paginate(10);

        $this->assign("list", $list);


        return $this->fetch();
    }

    //添加
    public function add(){
        if (request()->isGet())
        {
            $this->layoutModal();
            $this->templateTitle('添加单页');

            return $this->fetch();
        }

        if (request()->isAjax())
        {
            //创建数据
            $data = array(
                "create_time" => now_datetime(),
                "update_time" => now_datetime()
            );

            $data['title'] = request()->param('title');
            if(empty($data['title'])){
                return $this->ajaxError('请选填写标题！');
            }
            //是否显示
            $data["is_visible"] = request()->param("visible");
            //详细内容
            $data["content"] = request()->param("content");
                $result = Db::name('custom_page')
                    ->insertGetId($data);
                if ($result)
                {
                    $url=get_domain()."/info?id=".$result;
                    Db::name('custom_page')
                        ->where('id',$result)
                        ->update(
                            array(
                                "jump_url"=>$url
                            )
                        );
                    /*操作记录*/
                    Message::admin_log(0,"后台添加单页");
                    return $this->ajaxSuccess("添加成功！");
                }
                else
                {
                    $this->ajaxError('添加失败');
                }

        }
    }
    //编辑
    public function edit(){
        //ID
        $Id = request()->param("id");
        if (request()->isGet())
        {
            $this->layoutModal();

            //获取信息
            $info= Db::name("custom_page")
                ->where("id", $Id)
                ->find();
            $this->assign("info", $info);
            $this->assign("ueditor_content", array("content"=>$info["content"]));
            return $this->fetch();
        }

        if (request()->isAjax())
        {
            //创建数据
            $data = array(
                "update_time" => now_datetime(),
            );

            //广告图片
            $data["title"] = request()->param("title");
            if(empty($data['title'])){
                return $this->ajaxError('请填写标题！');
            }

            //内容
            $data["content"] = request()->param("content");
            //是否显示
            $data["is_visible"] = request()->param("visible");
                //更新分类ID路径
                Db::name("custom_page")
                    ->where("id", $Id)
                    ->update($data);
                /*操作记录*/
                Message::admin_log(0,"后台修改单页".$Id);
                return $this->ajaxSuccess("更新成功！");
        }
    }
    //删除
    public function remove(){
        if (request()->isAjax())
        {
            //获取ID
            $Id = request()->param("id");
            // 删除
            $result = Db::name('custom_page')
                ->where('id', $Id)
                ->delete();
            if($result){
                /*操作记录*/
                Message::admin_log(0,"后台删除单页");
                return $this->ajaxSuccess("删除成功！");
            }else{
                return $this->ajaxError("删除失败！");
            }

        }
    }

    //更改菜单显示隐藏
    public function visible()
    {
        if (request()->isAjax())
        {
            //菜单ID
            $id = request()->param("id");
            //获取可见性状态
            $status = request()->param("status");
                //修改菜单显示隐藏
                $save = Db::name("custom_page")
                    ->where("id", $id)
                    ->setField("is_visible", $status);

                if($save){
                    /*操作记录*/
                    Message::admin_log(0,"后台修改单页的状态");
                    return $this->ajaxSuccess("设置单页显示或隐藏成功！");
                }else{
                    return $this->ajaxError("设置单页显示或隐藏失败！");
                }
        }
    }
}